if(validation == undefined){
	var validation = {};
}
validation.clearErrorMessages = function(form) {
	var tables = form.getElementsByTagName("table");
	for(var k=0; k<tables.length; k++){
	    var table = tables[k];
	    if( typeof table == "undefined" ) {
	        table = form.childNodes[0];
	    }
	
		table.setAttribute("errored", false);
	    // clear out any rows with an "errorFor" attribute
	    var rows = table.rows;
	    var rowsToDelete = [];
	    if (rows == null){
	        return;
	    }
	
	    for(var i = 0; i < rows.length; i++) {
	        var r = rows[i];
	        if (r.getAttribute("errorFor")) {
	            rowsToDelete.push(r);
	        }
	        var tds = r.cells, td;
	        for(var j=0; j<tds.length; j++){
	        	td = tds[j];
	        	if(td.getAttribute("errorFor")){
	        		td.innerHTML = "";
	        	}	
	        }
	    }
	
	    // now delete the rows
	    for (var i = 0; i < rowsToDelete.length; i++) {
	        var r = rowsToDelete[i];
	        table.deleteRow(r.rowIndex);
	        //table.removeChild(rowsToDelete[i]); 
	    }
	}
}
validation.clearErrorLabels = function(form) {
    // set all labels back to the normal class
    var elements = form.getElementsByTagName("label");
    for (var i = 0; i < elements.length; i++) {
        var e = elements[i];
            var label = e;
            if (label && label.className == "errorLabel") {
                label.setAttribute("class", "label");
                label.setAttribute("className", "label"); //ie hack cause ie does not support setAttribute
            }
    }

}
validation.addError = function(e, errorText, errorTipType){
	if(errorTipType == undefined){
		errorTipType = 1;
	}
	if(errorTipType == 1){
		validation.addError1(e, errorText);
	}else if(errorTipType == 2){
		validation.addError2(e, errorText);
	}
}
/**
 * 该方法用来在input所在行的上一行显示错误提示信息
 */
validation.addError1 = function(e, errorText) {
	if(e == undefined){
		//alert("Can't find the input element for errorText '" + errorText + "'");
		return;
	}
    try {
    	var errorRow = validation.addErrorTipRow(e);
    	var td = validation.getParentColumn(e);
    	
    	var errorTipTd = validation.getErrorTipTd(errorRow, td);
    	var span = document.createElement("span");
        span.setAttribute("class", "errorMessage");
        span.setAttribute("className", "errorMessage"); //ie hack cause ie does not support setAttribute
        span.innerHTML += errorText + "  ";
		errorTipTd.appendChild(span);
		var labelTd = td.previousSibling;
		if(labelTd.tagName != "TD"){
			labelTd = labelTd.previousSibling;
		}
		if(labelTd != undefined){
		    var label = labelTd.getElementsByTagName("label")[0];
		    if(label){
			    label.setAttribute("class", "errorLabel");
				label.setAttribute("className", "errorLabel"); //ie hack cause ie does not support setAttribute
		    }
		}
    } catch (exception) {
        alert("validation.addError1:" + exception + "; name = " + e.name + ", errorText = " + errorText);
    }
}
validation.addErrorTipRow = function(e){
    try {
        var row = validation.getParentRow(e);
       
        var previousRow = validation.getPreviousRow(row);
        if(previousRow && previousRow.getAttribute("errorFor") == "true"){
        	return previousRow;
        }
        var table = row.parentNode;
        errorRow = document.createElement("tr");
        errorRow.setAttribute("errorFor", "true");
        var cells = row.cells;
        for(var i=0; i<=cells.length - 1; i+=2){
        	var nTD = document.createElement("td");
        	if((i+1) < cells.length){
	        	if(cells[i + 1].childNodes[1] == undefined){
	        		if(cells[i + 1].childNodes.length > 0){
	        			nTD.id = "errorFor" + cells[i + 1].childNodes[0].name;
	        		}
	        	}else{
	        		nTD.id = "errorFor" + cells[i + 1].childNodes[1].name;
	        	}
        	}
        	if((i+1) < cells.length){
        		nTD.colSpan = cells[i].colSpan + cells[i + 1].colSpan;
        	}else{
        		nTD.colSpan = cells[i].colSpan;
        	}
        	nTD.align = "center";
			errorRow.appendChild(nTD);        	
        }
        table.insertBefore(errorRow, row);
        return errorRow;
    }catch(e){
    	alert("validation.addErrorTipRow:" + e);
    }
}
/**
 * 该方法用来在input后面显示错误提示信息
 */
validation.addError2 = function(e, errorText) {
	if(e == undefined){
		alert("Can't find the input element for errorText '" + errorText + "'");
		return;
	}
    try {
    	if(!validation.isErrored(e)){
    		validation.addErrorTipTds(e);
    	}
    	var td = validation.getParentColumn(e);
    	
    	var errorTipTd = validation.getNextColumnTd(td);
    	var span = document.createElement("span");
        span.setAttribute("class", "errorMessage");
        span.setAttribute("className", "errorMessage"); //ie hack cause ie does not support setAttribute
        span.innerHTML += errorText + "  ";
		errorTipTd.appendChild(span);
		var labelTd = td.previousSibling;
		if(labelTd.tagName != "TD"){
			labelTd = labelTd.previousSibling;
		}
	    var label = labelTd.getElementsByTagName("label")[0];
	    if(label){
		    label.setAttribute("class", "errorLabel");
			label.setAttribute("className", "errorLabel"); //ie hack cause ie does not support setAttribute
	    }
    } catch (exception) {
        alert("validation.addError2:" + exception + "; " + e + ", errorText = " + errorText);
    }
}
validation.addErrorTipTds = function(e){
    try {
    	var row = validation.getParentRow(e);
        var td = validation.getParentColumn(e);
        
        var table = row.parentNode;
        var colgroup = table.parentNode.getElementsByTagName("COLGROUP")[0];
        var rows = table.rows;
        var tRow, tTds, tLen = 0;
        for(var i=0; i<rows.length; i++){
        	tRow = rows[i];
        	tTds = tRow.cells;
        	tLen = 0;
        	
        	for(var j=0, k=tTds.length; j<tTds.length; j++){
        		tLen += tTds[j].colSpan;
        		if(tLen % 2 == 0){
        			var errorTd = document.createElement("td");
		        	errorTd.setAttribute("errorFor", "true");
		        	errorTd.className = "tdError";
		        	if(j == tTds.length - 1){
		        		tRow.appendChild(errorTd);
		        	}else{
	        			tRow.insertBefore(errorTd, tTds[j + 1]);
		        	}
		        	j++;
		        	k++;
		        	if(i == 0){
			        	var errorCol = document.createElement("col");
			        	errorCol.className = "col" + k;
			        	colgroup.appendChild(errorCol);
		        	}
        		}
        	}
	        
        }
        table.setAttribute("errored", true);
    }catch(e){
    	alert("validation.addErrorTipTds:" + e);
    }
}
validation.isErrored = function(e){
	var result = false;
	var row = validation.getParentRow(e);
    var table = row.parentNode;
    if(table.getAttribute("errored")){
    	result = true;
    }
    return result;
}
validation.getPreviousRow = function(row){
	var table = row.parentNode;
	var rows = table.rows;
	for(var i=0; i<rows.length; i++){
		if(row == rows[i]){
			if(i > 0){
				return rows[i-1];
			}else{
				return null;
			}
		}
	}
}
validation.getParentRow = function(element){
	var result = element;
	if(result.length > 0){
		result = result[0];
	}
	if(result.tagName == "INPUT" && result.type== "checkbox"){
		while(result.tagName != "TABLE"){
			result = result.parentNode;
		}
	}
	
	while(result.tagName != "TR"){
		result = result.parentNode;
	}
	return result;
}
validation.getParentColumn = function(element){
	var result = element;
	if(result.length > 0){
		result = result[0];
	}
	if(result.tagName == "INPUT" && result.type== "checkbox"){
		while(result.tagName != "TABLE"){
			result = result.parentNode;
		}
	}
	while(result.tagName != "TD"){
		result = result.parentNode;
	}
	return result;
}
validation.getErrorTipTd = function(errorRow, td){
	var row = td.parentNode;
	var cells = row.cells;
	for(var i=0; i<cells.length; i++){
		if(cells[i] == td){
			var index = (i-1)/2;
			if(index < 0){
				index = 0;
			}
			index = Math.ceil(index);
			return errorRow.cells[index];
		}
	}
	return null;
}
validation.getElement = function(form, name){
	if(document[form][name] != null){
		return document[form][name];
	}else{
		return document.getElementById(name);
	}
}

validation.getNextColumnTd = function(td){
	var result = td.nextSibling;
	while(result.tagName != "TD"){
		result = result.nextSibling;
	}
	return result;
}

validation.addActionMessages = function(actionMessages, formId){
	if(actionMessages.length > 0){
		var form = $(formId);
		var ul = document.createElement("ul");
		ul.isTip = true;
		for(var i=0; i<actionMessages.length; i++){
			ul.innerHTML += "<li>" + '<span class="actionMessage">' + actionMessages[i] + '</span>' + "</li>";
		}
		form.insertBefore(ul, form.childNodes[0]);
	}
}
validation.clearActionMessagesAndErrors = function(formId){
	var form = $(formId);
	var elements = form.getElementsByTagName("ul");
	for(var i=0; i<elements.length; i++){
		if(elements[i].isTip){
			form.removeChild(elements[i]);
			i--;
		}
	}
}
validation.addActionErrors = function(actionErrors, formId){
	if(actionErrors.length > 0){
		var form = $(formId);
		var ul = document.createElement("ul");
		ul.isTip = true;
		for(var i=0; i<actionErrors.length; i++){
			ul.innerHTML += "<li>" + '<span class="errorMessage">' + actionErrors[i] + '</span>' + "</li>";
		}
		form.insertBefore(ul, form.childNodes[0]);
	}
}
validation.addFieldErrors = function(fieldErrors, formId){
	var form = $(formId);
	for(fieldName in fieldErrors){
		var e = form[fieldName];
		if(e)
			validation.addError(e, fieldErrors[fieldName]);
	}
}